//账号管理
import r from '@/utils/request';
import type { ResponseType, ListParams, ListResponseType } from './../types';
import { sm2 } from 'sm-crypto';
import { publicKeyAPI } from '../auth';
enum API {
    list = '/account/list', // 
    getUserInfo = '/account/getUserInfo',
    del = '/account/delete', // 
    add = '/account/add',
    putPhoto = '/account/updatePhoto',
    delAll = '/account/deleteAll', // 获取单条客户信息
    reserpwd = '/account/resetPwd',//重置密码
    listForAccount = '/role/listForAccount',
    Edit = '/account/update',
    UpdatePwd = '/account/updatePwd'
    // customerAdd = '/customer/add', // 潜在客户添加
    // customerUpdate = '/customer/update', // 潜在客户修改
}


// 账号管理列表返回值
export type accountlist = ListResponseType<{
    id: number;
    companyId: number;
    level: number;
    staffId: number;
    name: string;
    pwd: string;
    newPwd: null;
    type: number;
    enable: number;
    photo: string;
    mobile: string;
    username: string;
    roleIds: null;
    outLogs: null;
    verifyCode: null;
    verifyCodeId: null;
    openId: null;
}>
export interface AccountParam {
    id: number
}
export interface getUserInfoRes {
    code: number;
    msg: string;
    data: getUserInfoData;
}

export interface getUserInfoData {
    id: number;
    companyId: number;
    level: number;
    staffId: number;
    name: string;
    pwd: null;
    newPwd: null;
    type: number;
    enable: number;
    photo: string;
    mobile: string;
    username: string;
    roleIds: null;
    outLogs: null;
    verifyCode: null;
    verifyCodeId: null;
    openId: null;
}
export interface getUserInfoRes {
    id: number;
    username: string;
    name: string;
    pwd: string;
    photo: string;
}
interface Photo {
    photo: string
}
export interface ResetPwdItem {
    pwd: string;
    name?: string;
    newPwd: string;
    id?: number;
}

export interface AddaccountParams {
    id: number;
    enable: number;
    mobile: string;
    name: string;
    photo: string;
    pwd: string;
    roleIds: number[];
    username: string;
}
export interface ListForAccountParam {
    id: number;
    enable: number;
    mobile: string;
    name: string;
    photo: string;
    pwd: string;
    roleIds: number[];
    username: string;
}
export interface UpdatePwdParams {
    pwd: string
    newPwd: string
    newPwdTwo?: string
}
// 潜在客户列表接口
export function accountListAPI(params: ListParams): Promise<accountlist> {
    return r.get(API.list, params)
}
export function AcountGetuserinfoAPI(): Promise<getUserInfoRes> {
    return r.get(API.getUserInfo)
}
export function ListForAccountAPI(id: number): Promise<ListResponseType<ListForAccountParam>> {
    return r.get(API.listForAccount + '/' + id)
}


export function UpdatePhotoAPI(params: Photo): Promise<getUserInfoRes> {
    return r.put(API.putPhoto, params)
}
export function UpdatePwdAPI(params: UpdatePwdParams): Promise<accountlist> {
    return r.put(API.UpdatePwd, params)
}
export async function AddaccountAPI(data: AddaccountParams): Promise<ResponseType<AddaccountParams>> {
    const { data: publicKey } = await publicKeyAPI();
    return r.post(API.add, {
        ...data,
        pwd: sm2.doEncrypt(data.pwd, publicKey)
    });
    // return r.post(API.add, data);
}
export function ResetPwdAPI(params: ResetPwdItem): Promise<accountlist> {
    return r.put(API.reserpwd, params)
}



export async function AccountEditAPI(params: AddaccountParams): Promise<ResponseType<AddaccountParams>> {
    const { data: publicKey } = await publicKeyAPI();
    return r.put(API.Edit, {
        ...params,
        pwd: sm2.doEncrypt(params.pwd, publicKey)
    });
    // return r.put(API.Edit, params)
}
// 删除潜在客户接口
export function AccountDeleteAPI(id: number): Promise<ResponseType<string>> {
    return r.delete(API.del + '/' + id)
}
export function AccountDeleteAllAPI(id: number[]): Promise<ResponseType<null>> {
    return r.post(API.delAll, id);
}
